// 员工预约
import { getStaffList,getPositionList } from "../../../../services/index";

import {request} from '../../../../utils/http'

import {isEmptyObject} from "../../../../utils/utils"
interface IStaffViewProps {
  className: string
  sourceType: string
}

interface IStaffViewData {
  curTypeIndex?: number;
  staffItems?: Staff.IStaffItem[];
  menuTypeList?: {
    title: string;
    code: string;
  }[];
  positionItems?:  Staff.IStaffItem[]
}
const app = getApp()


Component<IStaffViewData, any, any>({
  data: {
    menuTypeList: [
      {
        title: "选择员工",
        code: "all"
      },
      // {
      //   title: "到店安排",
      //   code: "PROGRESS"
      // }
    ],
    positionItems: [],
    curTypeIndex: 0
  },
  props: {
    className: '',
    sourceType: 'indexPage',
    store_id:'',
  },
  onInit() {
    this.initData()
  },
  methods: {
    getPersonList(){
      request('/appointment/waiterList', 'get', {
        store_id:this.props.store_id,
        page:1
      }).then((res:any) => {
          console.log(res, 'person')
          if (res.code == 200) {
            let list = []

            res.data.data.forEach(item => {
              list.push({
                positionName: item.title,
                staffIcon:item.avatar,
                staffId: item.id,
                staffName: item.store_staff_name,
                staffNickName: item.name,
                staffServicePrice: item.price,
                staffSubscribeNum: item.appointment_count
              })
            })

            this.setData({
              staffItems: list
            });

          }
      })
    },
    initData() {
      console.log('store id', this.props.store_id)
      this.getPersonList()
      // getStaffList().then((res: Staff.IStaffItem[]) => {
      //   console.log("getStaffList", res);
      //   this.setData({
      //     staffItems: res
      //   });
      // });
      getPositionList().then(res => {
        console.log("getPositionList", res);
        this.setData({
          positionItems: res
        });
      })
    },
    setTempStaffData(item) {
      app.tempData.resStaffInfo = item
      const {resTimeInfo, resServiceInfo} = app.tempData
      if (!isEmptyObject(resTimeInfo) && resServiceInfo?.length) {
        // 确认页
        console.log(item, '099384884848488')

        my.navigateTo({
          url: '/pages/reservation/confirm-index/index'
        })
      } else {
        // TODO 预约时间
        console.log(item, 'jjjfllll')
        my.navigateTo({
          url: `/pages/reservation/time-index/index?id=${item.staffId}`
        })
      }
    },
    // 点击预约
    handleTap(item) {
      console.log('staff::handleTap:', item)
      this.setTempStaffData(item)

    },
    // 到店安排
    handleSelect({target: {dataset: { item }}}) {
      console.log('staff::handleSelect:', item)
      this.setTempStaffData(item)
    },
    tabItemClick({ index }) {
      if (this.data.curTypeIndex === index) {
        return;
      }
      this.setData({
        curTypeIndex: index
      });
    }
  }
});
